home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / ntpath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  11.6 KB  |  485 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Common pathname manipulations, WindowsNT/95 version.
  5.  
  6. Instead of importing this module directly, import os and refer to this
  7. module as os.path.
  8. '''
  9. import os
  10. import stat
  11. import sys
  12. __all__ = [
  13.     'normcase',
  14.     'isabs',
  15.     'join',
  16.     'splitdrive',
  17.     'split',
  18.     'splitext',
  19.     'basename',
  20.     'dirname',
  21.     'commonprefix',
  22.     'getsize',
  23.     'getmtime',
  24.     'getatime',
  25.     'getctime',
  26.     'islink',
  27.     'exists',
  28.     'lexists',
  29.     'isdir',
  30.     'isfile',
  31.     'ismount',
  32.     'walk',
  33.     'expanduser',
  34.     'expandvars',
  35.     'normpath',
  36.     'abspath',
  37.     'splitunc',
  38.     'curdir',
  39.     'pardir',
  40.     'sep',
  41.     'pathsep',
  42.     'defpath',
  43.     'altsep',
  44.     'extsep',
  45.     'devnull',
  46.     'realpath',
  47.     'supports_unicode_filenames']
  48. curdir = '.'
  49. pardir = '..'
  50. extsep = '.'
  51. sep = '\\'
  52. pathsep = ';'
  53. altsep = '/'
  54. defpath = '.;C:\\bin'
  55. if 'ce' in sys.builtin_module_names:
  56.     defpath = '\\Windows'
  57. elif 'os2' in sys.builtin_module_names:
  58.     altsep = '/'
  59.  
  60. devnull = 'nul'
  61.  
  62. def normcase(s):
  63.     '''Normalize case of pathname.
  64.  
  65.     Makes all characters lowercase and all slashes into backslashes.'''
  66.     return s.replace('/', '\\').lower()
  67.  
  68.  
  69. def isabs(s):
  70.     '''Test whether a path is absolute'''
  71.     s = splitdrive(s)[1]
  72.     if s != '':
  73.         pass
  74.     return s[:1] in '/\\'
  75.  
  76.  
  77. def join(a, *p):
  78.     '''Join two or more pathname components, inserting "\\" as needed'''
  79.     path = a
  80.     for b in p:
  81.         b_wins = 0
  82.         if path == '':
  83.             b_wins = 1
  84.         elif isabs(b):
  85.             if path[1:2] != ':' or b[1:2] == ':':
  86.                 b_wins = 1
  87.             elif (len(path) > 3 or len(path) == 3) and path[-1] not in '/\\':
  88.                 b_wins = 1
  89.             
  90.         
  91.         if b_wins:
  92.             path = b
  93.             continue
  94.         if not len(path) > 0:
  95.             raise AssertionError
  96.         if path[-1] in '/\\':
  97.             if b and b[0] in '/\\':
  98.                 path += b[1:]
  99.             else:
  100.                 path += b
  101.         b[0] in '/\\'
  102.         if path[-1] == ':':
  103.             path += b
  104.             continue
  105.         if b:
  106.             if b[0] in '/\\':
  107.                 path += b
  108.             else:
  109.                 path += '\\' + b
  110.         b[0] in '/\\'
  111.         path += '\\'
  112.     
  113.     return path
  114.  
  115.  
  116. def splitdrive(p):
  117.     '''Split a pathname into drive and path specifiers. Returns a 2-tuple
  118. "(drive,path)";  either part may be empty'''
  119.     if p[1:2] == ':':
  120.         return (p[0:2], p[2:])
  121.     
  122.     return ('', p)
  123.  
  124.  
  125. def splitunc(p):
  126.     """Split a pathname into UNC mount point and relative path specifiers.
  127.  
  128.     Return a 2-tuple (unc, rest); either part may be empty.
  129.     If unc is not empty, it has the form '//host/mount' (or similar
  130.     using backslashes).  unc+rest is always the input path.
  131.     Paths containing drive letters never have an UNC part.
  132.     """
  133.     if p[1:2] == ':':
  134.         return ('', p)
  135.     
  136.     firstTwo = p[0:2]
  137.     if firstTwo == '//' or firstTwo == '\\\\':
  138.         normp = normcase(p)
  139.         index = normp.find('\\', 2)
  140.         if index == -1:
  141.             return ('', p)
  142.         
  143.         index = normp.find('\\', index + 1)
  144.         if index == -1:
  145.             index = len(p)
  146.         
  147.         return (p[:index], p[index:])
  148.     
  149.     return ('', p)
  150.  
  151.  
  152. def split(p):
  153.     '''Split a pathname.
  154.  
  155.     Return tuple (head, tail) where tail is everything after the final slash.
  156.     Either part may be empty.'''
  157.     (d, p) = splitdrive(p)
  158.     i = len(p)
  159.     while i and p[i - 1] not in '/\\':
  160.         i = i - 1
  161.     head = p[:i]
  162.     tail = p[i:]
  163.     head2 = head
  164.     while head2 and head2[-1] in '/\\':
  165.         head2 = head2[:-1]
  166.     if not head2:
  167.         pass
  168.     head = head
  169.     return (d + head, tail)
  170.  
  171.  
  172. def splitext(p):
  173.     '''Split the extension from a pathname.
  174.  
  175.     Extension is everything from the last dot to the end.
  176.     Return (root, ext), either part may be empty.'''
  177.     i = p.rfind('.')
  178.     if i <= max(p.rfind('/'), p.rfind('\\')):
  179.         return (p, '')
  180.     else:
  181.         return (p[:i], p[i:])
  182.  
  183.  
  184. def basename(p):
  185.     '''Returns the final component of a pathname'''
  186.     return split(p)[1]
  187.  
  188.  
  189. def dirname(p):
  190.     '''Returns the directory component of a pathname'''
  191.     return split(p)[0]
  192.  
  193.  
  194. def commonprefix(m):
  195.     '''Given a list of pathnames, returns the longest common leading component'''
  196.     if not m:
  197.         return ''
  198.     
  199.     prefix = m[0]
  200.     for item in m:
  201.         for i in range(len(prefix)):
  202.             if prefix[:i + 1] != item[:i + 1]:
  203.                 prefix = prefix[:i]
  204.                 if i == 0:
  205.                     return ''
  206.                 
  207.                 break
  208.                 continue
  209.         
  210.     
  211.     return prefix
  212.  
  213.  
  214. def getsize(filename):
  215.     '''Return the size of a file, reported by os.stat()'''
  216.     return os.stat(filename).st_size
  217.  
  218.  
  219. def getmtime(filename):
  220.     '''Return the last modification time of a file, reported by os.stat()'''
  221.     return os.stat(filename).st_mtime
  222.  
  223.  
  224. def getatime(filename):
  225.     '''Return the last access time of a file, reported by os.stat()'''
  226.     return os.stat(filename).st_atime
  227.  
  228.  
  229. def getctime(filename):
  230.     '''Return the creation time of a file, reported by os.stat().'''
  231.     return os.stat(filename).st_ctime
  232.  
  233.  
  234. def islink(path):
  235.     '''Test for symbolic link.  On WindowsNT/95 always returns false'''
  236.     return False
  237.  
  238.  
  239. def exists(path):
  240.     '''Test whether a path exists'''
  241.     
  242.     try:
  243.         st = os.stat(path)
  244.     except os.error:
  245.         return False
  246.  
  247.     return True
  248.  
  249. lexists = exists
  250.  
  251. def isdir(path):
  252.     '''Test whether a path is a directory'''
  253.     
  254.     try:
  255.         st = os.stat(path)
  256.     except os.error:
  257.         return False
  258.  
  259.     return stat.S_ISDIR(st.st_mode)
  260.  
  261.  
  262. def isfile(path):
  263.     '''Test whether a path is a regular file'''
  264.     
  265.     try:
  266.         st = os.stat(path)
  267.     except os.error:
  268.         return False
  269.  
  270.     return stat.S_ISREG(st.st_mode)
  271.  
  272.  
  273. def ismount(path):
  274.     '''Test whether a path is a mount point (defined as root of drive)'''
  275.     (unc, rest) = splitunc(path)
  276.     if unc:
  277.         return rest in ('', '/', '\\')
  278.     
  279.     p = splitdrive(path)[1]
  280.     if len(p) == 1:
  281.         pass
  282.     return p[0] in '/\\'
  283.  
  284.  
  285. def walk(top, func, arg):
  286.     """Directory tree walk with callback function.
  287.  
  288.     For each directory in the directory tree rooted at top (including top
  289.     itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
  290.     dirname is the name of the directory, and fnames a list of the names of
  291.     the files and subdirectories in dirname (excluding '.' and '..').  func
  292.     may modify the fnames list in-place (e.g. via del or slice assignment),
  293.     and walk will only recurse into the subdirectories whose names remain in
  294.     fnames; this can be used to implement a filter, or to impose a specific
  295.     order of visiting.  No semantics are defined for, or required of, arg,
  296.     beyond that arg is always passed to func.  It can be used, e.g., to pass
  297.     a filename pattern, or a mutable object designed to accumulate
  298.     statistics.  Passing None for arg is common."""
  299.     
  300.     try:
  301.         names = os.listdir(top)
  302.     except os.error:
  303.         return None
  304.  
  305.     func(arg, top, names)
  306.     exceptions = ('.', '..')
  307.     for name in names:
  308.         if name not in exceptions:
  309.             name = join(top, name)
  310.             if isdir(name):
  311.                 walk(name, func, arg)
  312.             
  313.         isdir(name)
  314.     
  315.  
  316.  
  317. def expanduser(path):
  318.     '''Expand ~ and ~user constructs.
  319.  
  320.     If user or $HOME is unknown, do nothing.'''
  321.     if path[:1] != '~':
  322.         return path
  323.     
  324.     i = 1
  325.     n = len(path)
  326.     while i < n and path[i] not in '/\\':
  327.         i = i + 1
  328.     if i == 1:
  329.         if 'HOME' in os.environ:
  330.             userhome = os.environ['HOME']
  331.         elif 'HOMEPATH' not in os.environ:
  332.             return path
  333.         else:
  334.             
  335.             try:
  336.                 drive = os.environ['HOMEDRIVE']
  337.             except KeyError:
  338.                 drive = ''
  339.  
  340.             userhome = join(drive, os.environ['HOMEPATH'])
  341.     else:
  342.         return path
  343.     return userhome + path[i:]
  344.  
  345.  
  346. def expandvars(path):
  347.     '''Expand shell variables of form $var and ${var}.
  348.  
  349.     Unknown variables are left unchanged.'''
  350.     if '$' not in path:
  351.         return path
  352.     
  353.     import string
  354.     varchars = string.ascii_letters + string.digits + '_-'
  355.     res = ''
  356.     index = 0
  357.     pathlen = len(path)
  358.     while index < pathlen:
  359.         c = path[index]
  360.         if c == "'":
  361.             path = path[index + 1:]
  362.             pathlen = len(path)
  363.             
  364.             try:
  365.                 index = path.index("'")
  366.                 res = res + "'" + path[:index + 1]
  367.             except ValueError:
  368.                 res = res + path
  369.                 index = pathlen - 1
  370.             except:
  371.                 None<EXCEPTION MATCH>ValueError
  372.             
  373.  
  374.         None<EXCEPTION MATCH>ValueError
  375.         if c == '$':
  376.             if path[index + 1:index + 2] == '$':
  377.                 res = res + c
  378.                 index = index + 1
  379.             elif path[index + 1:index + 2] == '{':
  380.                 path = path[index + 2:]
  381.                 pathlen = len(path)
  382.                 
  383.                 try:
  384.                     index = path.index('}')
  385.                     var = path[:index]
  386.                     if var in os.environ:
  387.                         res = res + os.environ[var]
  388.                 except ValueError:
  389.                     res = res + path
  390.                     index = pathlen - 1
  391.                 except:
  392.                     None<EXCEPTION MATCH>ValueError
  393.                 
  394.  
  395.             None<EXCEPTION MATCH>ValueError
  396.             var = ''
  397.             index = index + 1
  398.             c = path[index:index + 1]
  399.             while c != '' and c in varchars:
  400.                 var = var + c
  401.                 index = index + 1
  402.                 c = path[index:index + 1]
  403.             if var in os.environ:
  404.                 res = res + os.environ[var]
  405.             
  406.             if c != '':
  407.                 res = res + c
  408.             
  409.         else:
  410.             res = res + c
  411.         index = index + 1
  412.     return res
  413.  
  414.  
  415. def normpath(path):
  416.     '''Normalize path, eliminating double slashes, etc.'''
  417.     path = path.replace('/', '\\')
  418.     (prefix, path) = splitdrive(path)
  419.     if prefix == '':
  420.         while path[:1] == '\\':
  421.             prefix = prefix + '\\'
  422.             path = path[1:]
  423.     elif path.startswith('\\'):
  424.         prefix = prefix + '\\'
  425.         path = path.lstrip('\\')
  426.     
  427.     comps = path.split('\\')
  428.     i = 0
  429.     while i < len(comps):
  430.         if comps[i] in ('.', ''):
  431.             del comps[i]
  432.             continue
  433.         if comps[i] == '..':
  434.             if i > 0 and comps[i - 1] != '..':
  435.                 del comps[i - 1:i + 1]
  436.                 i -= 1
  437.             elif i == 0 and prefix.endswith('\\'):
  438.                 del comps[i]
  439.             else:
  440.                 i += 1
  441.         prefix.endswith('\\')
  442.         i += 1
  443.     if not prefix and not comps:
  444.         comps.append('.')
  445.     
  446.     return prefix + '\\'.join(comps)
  447.  
  448.  
  449. def abspath(path):
  450.     '''Return the absolute version of a path'''
  451.     global abspath
  452.     
  453.     try:
  454.         _getfullpathname = _getfullpathname
  455.         import nt
  456.     except ImportError:
  457.         
  458.         def _abspath(path):
  459.             if not isabs(path):
  460.                 path = join(os.getcwd(), path)
  461.             
  462.             return normpath(path)
  463.  
  464.         abspath = _abspath
  465.         return _abspath(path)
  466.  
  467.     if path:
  468.         
  469.         try:
  470.             path = _getfullpathname(path)
  471.         except WindowsError:
  472.             pass
  473.         except:
  474.             None<EXCEPTION MATCH>WindowsError
  475.         
  476.  
  477.     None<EXCEPTION MATCH>WindowsError
  478.     path = os.getcwd()
  479.     return normpath(path)
  480.  
  481. realpath = abspath
  482. if hasattr(sys, 'getwindowsversion'):
  483.     pass
  484. supports_unicode_filenames = sys.getwindowsversion()[3] >= 2
  485.